//
//  ConsulationAskCell.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/2.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ConsulationAskCell.h"

#import <PYPhotoBrowser/PYPhotoBrowser.h>

@interface ConsulationAskCell ()

/// viewModle
@property (nonatomic, readwrite, strong) ConsulationAskCellModel *viewModel;

/// IBOutlet
@property (weak, nonatomic) IBOutlet UILabel* titleLabel;
@property (weak, nonatomic) IBOutlet UILabel* contentLabel;
@property (weak, nonatomic) IBOutlet UILabel* nameLabel;
@property (weak, nonatomic) IBOutlet UILabel* timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView* avatarImgView;
@property (weak, nonatomic) IBOutlet UIView* photosView;

@end

@implementation ConsulationAskCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - bind data
- (void)bindViewModel:(ConsulationAskCellModel *)viewModel
{
    self.viewModel = viewModel;
    
    @weakify(self);
    
    self.titleLabel.text = self.viewModel.title;
    self.contentLabel.text = self.viewModel.content;
    self.nameLabel.text = self.viewModel.name;
    self.timeLabel.text = self.viewModel.time;
    [self.avatarImgView yy_setImageWithURL:[NSURL URLWithString:self.viewModel.imgurl] placeholder:MHImageNamed(@"placeholderLogo")];
    
    [[self rac_signalForSelector:@selector(layoutSubviews)] subscribeNext:^(id x)
     {
         @strongify(self);
         
         self.avatarImgView.layer.cornerRadius = self.avatarImgView.frame.size.width/2.0f;
         self.avatarImgView.layer.masksToBounds = YES;
     }];
    
    BOOL isHaveImage = self.viewModel.image!=nil && [self.viewModel.image isNotBlank];
    if (isHaveImage)
    {
        [self.photosView removeAllSubviews];
        
        NSInteger imgColNum = 3;
        
        // 计算大小
        CGFloat imgOffset = 16;
        CGFloat imgSize = (self.photosView.frame.size.width - ((imgColNum + 1)*imgOffset)) / imgColNum;
        
        PYPhotosView *photosView = [PYPhotosView photosViewWithImages:[NSMutableArray array]];
        photosView.photosState = PYPhotosViewStateDidCompose;
        photosView.photosMaxCol = imgColNum;
        photosView.oneImageFullFrame = NO;
        photosView.autoLayoutWithWeChatSytle = YES;
        photosView.autoSetPhotoState = NO;
        photosView.photoWidth = imgSize;
        photosView.photoHeight = imgSize;
        photosView.photoMargin = imgOffset;
        
        NSInteger row = 1;
        photosView.frame = (CGRect) {0, 0, self.photosView.frame.size.width, imgSize*row + imgOffset*(row - 1)};
        photosView.tag = 12;
        [self.photosView addSubview:photosView];
        
        photosView.originalUrls = @[self.viewModel.image];
        
        //修改约束
        for (NSLayoutConstraint* curConstraint in self.photosView.constraints)
        {
            if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
            {
                curConstraint.constant = photosView.frame.size.height;
            }
            else if (curConstraint.firstAttribute == NSLayoutAttributeTop)
            {
                curConstraint.constant = 12;
            }
        }
    }
    else
    {
        for (NSLayoutConstraint* curConstraint in self.photosView.constraints)
        {
            if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
            {
                curConstraint.constant = 0;
            }
            else if (curConstraint.firstAttribute == NSLayoutAttributeTop)
            {
                curConstraint.constant = 0;
            }
        }
    }
}

@end
